package com.hanlin.backend.controller;

import com.hanlin.backend.common.entity.HlUser;
import com.hanlin.backend.common.error.CommonException;
import com.hanlin.backend.common.util.MD5Utils;
import com.hanlin.backend.common.util.RedisUtil;
import com.hanlin.backend.common.util.ResultCommon;
import com.hanlin.backend.dto.PassWordDto;
import com.hanlin.backend.dto.UserDto;
import com.hanlin.backend.service.LoginService;
import com.hanlin.backend.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 登录的controller
 *
 * @author:hl.yuan
 * @date：2020-12-24
 */
@RestController
@Slf4j
@Api(value = "/frontend/api", tags = "登录")
public class LoginController {


    @Autowired
    private LoginService loginService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 登录接口
     *
     * @param userDto
     * @return
     */
    @PostMapping("/frontend/api/login")
    @ApiOperation("登录接口")
    public ResultCommon login(@RequestBody UserDto userDto,HttpServletRequest request) {
        String username = userDto.getUsername();
        String password = userDto.getPassword();
        password = MD5Utils.encrypt(username, password);
        ResultCommon common = loginService.login(username, password, request);
        return common;
    }

    /**
     * 接口测试
     *
     * @return
     */
    @GetMapping("/")
    @ApiOperation("接口测试")
    public ResultCommon ping() {
        ResultCommon resultCommon = new ResultCommon();
        resultCommon.setMessage("请求成功，后端服务正常");
        return resultCommon;
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @GetMapping("/frontend/api/user/info")
    @ApiOperation("获取用户信息")
    public ResultCommon<HlUser> getUserInfo(HttpServletRequest request) {
        ResultCommon resultCommon = new ResultCommon();
        HlUser user = tokenService.getUserByRequest(request);
        resultCommon.setData(user);
        return resultCommon;
    }

    /**
     * 登出
     *
     * @return
     */
    @PostMapping("/frontend/api/user/logout")
    @ApiOperation("登出")
    public ResultCommon logout(HttpServletRequest request) {
        SecurityUtils.getSubject().logout();
        HlUser user = tokenService.getUserByRequest(request);
        // 设置redis的会话，立马过期
        redisUtil.expire("user_" + user.getUsername(), 1L);
        return new ResultCommon();
    }

    @PostMapping("/frontend/api/user/updatePassWord")
    @ApiOperation("修改密码")
    public ResultCommon updatePassWord(HttpServletRequest request, @RequestBody PassWordDto passWord) {
        ResultCommon resultCommon = new ResultCommon();
        try {
            HlUser user = tokenService.getUserByRequest(request);
            loginService.updatePassWord(user, passWord.getPassWord(), passWord.getPassWordNew1(), passWord.getPassWordNew2());
        } catch (CommonException ex) {
            resultCommon.setCode("500");
            resultCommon.setMessage(ex.getMessage());
            log.error("密码修改失败", ex);
        }
        return resultCommon;
    }

    @GetMapping("/frontend/api/user/resetPassWord")
    @ApiOperation("重置密码")
    public ResultCommon resetPassWord(@ApiParam(required = true, name = "用户ID") Integer userId) {
        ResultCommon resultCommon = new ResultCommon();
        try {
            String passWord = loginService.resetPassWord(userId);
            resultCommon.setData("密码重置成功，新密码为：" + passWord);
        } catch (Exception ex) {
            resultCommon.setCode("500");
            resultCommon.setMessage(ex.getMessage());
            log.error("密码重置失败", ex);
        }
        return resultCommon;
    }


}
